﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;

namespace EnglishAuction
{
    public enum EmployeeType
    {
        NhanVienNhanSu = 1,
        NhanVienQuanLy = 2,
    }

    public class Utilities
    {
        public static double Currency(string price)
        {
            string t = price.Split(new char[] { '.' })[0];
            t = t.Trim(new char[] { ',' });
            return double.Parse(t);
        }

        public static bool AuthencationAdmin(HttpResponse reponse, EmployeeType loaiNhanVien, NhanVien nhanVien)
        {
            if (nhanVien == null)
            {
                //reponse.Redirecst("AccessDeny.aspx");
                return false;
            }

            return true;
        }

        public static string GeneratePassword(int lenght)
        {
            string allowedChars = "";
            allowedChars = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,";
            allowedChars += "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,";
            allowedChars += "1,2,3,4,5,6,7,8,9,0,!,@,#,$,%,&,?";
            char[] sep = { ',' };
            string[] arr = allowedChars.Split(sep);
            string passwordString = "";
            string temp = "";
            Random rand = new Random();
            for (int i = 0; i < lenght; i++)
            {
                temp = arr[rand.Next(0, arr.Length)];
                passwordString += temp;
            }

            return passwordString;
        }

        public static string GetJsonDate(List<SanPham> lst, int start)
        {
            int s = Default.NUMBER_PRODUCT_ONPAGE * (start - 1);
            int e = s + Default.NUMBER_PRODUCT_ONPAGE;
            if (e > lst.Count)
                e = lst.Count;

            var jsonDate = "[";
            var i = 0;
            for (; s < e; s++)
            {
                lst[s].Countdown = String.Format("<div id='txtCountdown{0}'></div>", i + 1);
                var dateTmp = lst[s].NgayLenSan.Value.AddDays(lst[s].ThoiGianDauGia.Value);
                if (s <= e - 2)
                    jsonDate += "\"" + String.Format("{0:MMMM dd, yyyy HH:mm:ss}", dateTmp) + "\", ";
                else
                    jsonDate += "\"" + String.Format("{0:MMMM dd, yyyy HH:mm:ss}", dateTmp) + "\" ";
                i++;
            }



            /*foreach (var item in lst)
            {
                item.Countdown = String.Format("<div id='txtCountdown{0}'></div>", i + 1);
                var dateTmp = item.NgayLenSan.Value.AddDays(item.ThoiGianDauGia.Value);
                if (i <= lst.Count() - 2)
                    jsonDate += "\"" + String.Format("{0:MMMM dd, yyyy HH:mm:ss}", dateTmp) + "\", ";
                else
                    jsonDate += "\"" + String.Format("{0:MMMM dd, yyyy HH:mm:ss}", dateTmp) + "\" ";
                i++;
            }*/
            jsonDate += "]";

            return jsonDate;
        }

        public static string GetMD5Hash(string input)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);
            bs = x.ComputeHash(bs);
            System.Text.StringBuilder s = new System.Text.StringBuilder();
            foreach (byte b in bs)
            {
                s.Append(b.ToString("x2").ToLower());
            }
            string password = s.ToString();
            return password;
        }

        public static void SendMail(string mailto, string body, string title = "Cấp lại mật khẩu")
        {
            // Gmail Address from where you send the mail
            var fromAddress = "nguyennhuth2008@gmail.com";
            // any address where the email will be sending
            var toAddress = mailto;
            //Password of your gmail address
            const string fromPassword = "nguyennhu";
            // Passing the values and make a email formate to display
            string subject = title;
            // smtp settings
            var smtp = new System.Net.Mail.SmtpClient();
            {
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.EnableSsl = true;
                smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
                smtp.Timeout = 20000;
            }
            // Passing values to smtp object
            smtp.Send(fromAddress, toAddress, subject, body);
        }
    }
}